package com.bosssoft.hr.train.j2se.collection;

import com.bosssoft.hr.train.j2se.pojo.entity.User;

import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

/**
 * TreeSetDemo
 */
public class TreeSetDemo {
    private Set<User> userTreeSet = new TreeSet<>(new Comparator<User>() {
        @Override
        public int compare(User o1, User o2) {
            return o1.getCode().compareTo(o2.getCode());
        }
    });
    User[] users = new User[10];

    /**
     * 创建测试数据
     * @return 用户数组
     */
    public User[] getUsers() {
        for (int i = 9; i >= 0; i--) {
            users[i] = new User(new Long(i), "BossSoft" + i, "用户" + i);
        }
        return users;
    }

    /**
     * 排序
     * @param users
     * @return
     */
    public User[] sort(User[] users){
        if(users.length>0 && users!=null){
            for (User user : users){
                userTreeSet.add(user);
            }
            User[] res = userTreeSet.toArray(new User[userTreeSet.size()]);
            System.out.println("排序成功");
            return res;
        }else{
            return users;
        }
    }

    /**
     * 遍历
     */
    public void visitUserTreeSet(){
        users = getUsers();
        sort(users);
        for (User user : users){
            System.out.println("用户信息"+user);
        }
        System.out.println(123123123);
        System.out.println(32133123);
        System.out.println("asdasda");
        System.out.println("123123123123");
    }

}
